package 算法;

import java.util.Stack;

/**
 * @author 乐心湖
 * @date 2020/6/20 0:37
 **/
public class ValidParentheses {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char a = s.charAt(i);
            if (a == '(' || a == '{' || a == '[') {
                stack.push(a);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                Character pop = stack.pop();
                if (a == ')' && pop != '(') {
                    return false;
                }
                if (a == '}' && pop != '{') {
                    return false;
                }
                if (a == ']' && pop != '[') {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
